package com.easemob.cloud;

import android.util.Pair;
import com.easemob.chat.EMChat;
import com.easemob.chat.core.f;
import com.easemob.chat.core.f.b;
import com.easemob.chat.core.f.c;
import com.easemob.chat.core.k;
import com.easemob.exceptions.EMNetworkUnconnectedException;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.EMLog;
import com.easemob.util.NetUtils;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SocketFactory;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpClientManager
{
  private static final String TAG = "HttpClientManager";
  public static String Method_GET = "GET";
  public static String Method_POST = "POST";
  public static String Method_PUT = "PUT";
  public static String Method_DELETE = "DELETE";
  public static final int max_retries_times_on_connection_refused = 20;

  public static String sendHttpRequest(String paramString1, Map<String, String> paramMap, String paramString2, String paramString3)
    throws EaseMobException, IOException
  {
    String str1 = null;
    Object localObject1 = null;
    Object localObject2 = null;
    String str2 = paramString1;
    for (int i = 0; i < 20; i++)
    {
      EMLog.d("HttpClientManager", "try send request, request url: " + paramString1 + " with number: " + i);
      localObject1 = null;
      localObject2 = null;
      Object localObject3 = null;
      try
      {
        str1 = sendHttpRequestWithCountDown(str2, paramMap, paramString2, paramString3);
      }
      catch (IOException localIOException)
      {
        localObject1 = localIOException;
        localObject3 = localIOException;
      }
      catch (EaseMobException localEaseMobException)
      {
        localObject2 = localEaseMobException;
        localObject3 = localEaseMobException;
      }
      String str3 = "";
      if ((localObject3 != null) && (localObject3.getMessage() != null))
        str3 = localObject3.getMessage();
      if ((!k.a().d()) || (!str3.toLowerCase().contains("refused")) || (!NetUtils.hasNetwork(EMChat.getInstance().getAppContext())))
        break;
      f.c localc = f.a().g();
      str2 = getNewHost(paramString1, localc);
    }
    if (localObject1 != null)
      throw localObject1;
    if (localObject2 != null)
      throw localObject2;
    return str1;
  }

  public static Pair<Integer, String> sendRequest(String paramString1, Map<String, String> paramMap, String paramString2, String paramString3)
    throws IOException, EaseMobException
  {
    Pair localPair = null;
    Object localObject1 = null;
    Object localObject2 = null;
    String str1 = paramString1;
    for (int i = 0; i < 20; i++)
    {
      EMLog.d("HttpClientManager", "try send request, request url: " + paramString1 + " with number: " + i);
      localObject1 = null;
      localObject2 = null;
      Object localObject3 = null;
      try
      {
        HttpResponse localHttpResponse = sendRequestWithCountDown(str1, paramMap, paramString2, paramString3);
        localObject4 = localHttpResponse.getEntity();
        if (localObject4 != null)
        {
          String str3 = EntityUtils.toString((HttpEntity)localObject4, "UTF-8");
          localPair = new Pair(Integer.valueOf(localHttpResponse.getStatusLine().getStatusCode()), str3);
        }
      }
      catch (IOException localIOException)
      {
        localObject1 = localIOException;
        localObject3 = localIOException;
      }
      catch (EaseMobException localEaseMobException)
      {
        localObject2 = localEaseMobException;
        localObject3 = localEaseMobException;
      }
      String str2 = "";
      if ((localObject3 != null) && (localObject3.getMessage() != null))
        str2 = localObject3.getMessage();
      if ((!k.a().d()) || (!str2.toLowerCase().contains("refused")) || (!NetUtils.hasNetwork(EMChat.getInstance().getAppContext())))
        break;
      Object localObject4 = f.a().g();
      str1 = getNewHost(paramString1, (f.c)localObject4);
    }
    if (localObject1 != null)
      throw localObject1;
    if (localObject2 != null)
      throw localObject2;
    return localPair;
  }

  private static HttpResponse sendRequestWithCountDown(String paramString1, Map<String, String> paramMap, String paramString2, String paramString3)
    throws EaseMobException, IOException
  {
    HttpResponse localHttpResponse = null;
    try
    {
      localHttpResponse = getHttpResponse(paramString1, paramMap, paramString2, paramString3);
    }
    catch (IOException localIOException)
    {
      throw localIOException;
    }
    catch (Exception localException)
    {
      String str = "http request failed : " + paramString1;
      if ((localException != null) && (localException.getMessage() != null))
        str = localException.getMessage();
      if (str.contains("Unable to resolve host"))
        throw new EMNetworkUnconnectedException(-1001, "EMNetworkUnconnectedException:Unable to resolve host");
      throw new EaseMobException(-1003, str);
    }
    return localHttpResponse;
  }

  private static String sendHttpRequestWithCountDown(String paramString1, Map<String, String> paramMap, String paramString2, String paramString3)
    throws EaseMobException, IOException
  {
    String str = null;
    try
    {
      HttpResponse localHttpResponse = getHttpResponse(paramString1, paramMap, paramString2, paramString3);
      localObject = localHttpResponse.getEntity();
      if (localObject != null)
        str = EntityUtils.toString((HttpEntity)localObject, "UTF-8");
    }
    catch (IOException localIOException)
    {
      throw localIOException;
    }
    catch (Exception localException)
    {
      Object localObject = "http request failed : " + paramString1;
      if ((localException != null) && (localException.getMessage() != null))
        localObject = localException.getMessage();
      if (((String)localObject).contains("Unable to resolve host"))
        throw new EMNetworkUnconnectedException(-1001, "EMNetworkUnconnectedException:Unable to resolve host");
      throw new EaseMobException(-1003, (String)localObject);
    }
    return str;
  }

  static String getNewHost(String paramString, f.c paramc)
  {
    String str1 = paramc.a;
    int i = paramc.b;
    String str2 = paramc.c;
    String str3 = str2 + "://" + str1 + ":" + i;
    String str4 = paramString.substring(paramString.indexOf("/", 8));
    String str5 = str3 + str4;
    return str5;
  }

  static Map<String, String> addDomainToHeaders(Map<String, String> paramMap)
  {
    if (k.a().d())
    {
      f.c localc = f.a().c();
      if ((localc != null) && (localc.d != null) && (localc.c != null) && (localc.c.contains("https")) && (localc.d.a != null) && (!localc.d.a.trim().equals("")))
      {
        if (paramMap == null)
          paramMap = new HashMap();
        paramMap.put("Host", localc.d.a);
      }
    }
    return paramMap;
  }

  static void checkAndProcessSSL(String paramString, DefaultHttpClient paramDefaultHttpClient)
    throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, KeyManagementException, UnrecoverableKeyException
  {
    if ((paramString != null) && (paramString.startsWith("https")))
    {
      KeyStore localKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
      localKeyStore.load(null, null);
      MySSLSocketFactory localMySSLSocketFactory = new MySSLSocketFactory(localKeyStore);
      localMySSLSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      paramDefaultHttpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", localMySSLSocketFactory, 443));
    }
  }

  public static HttpResponse getHttpResponse(String paramString1, Map<String, String> paramMap, String paramString2, String paramString3)
    throws KeyStoreException, KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, IOException
  {
    paramMap = addDomainToHeaders(paramMap);
    return httpExecute(paramString1, paramMap, paramString2, paramString3);
  }

  public static HttpResponse httpExecute(String paramString1, Map<String, String> paramMap, String paramString2, String paramString3)
    throws ClientProtocolException, IOException, KeyStoreException, KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException
  {
    HttpResponse localHttpResponse = null;
    DefaultHttpClient localDefaultHttpClient = HttpClientConfig.getDefaultHttpClient();
    Object localObject2;
    if ((paramString1 != null) && (paramString1.startsWith("https")))
    {
      localObject1 = KeyStore.getInstance(KeyStore.getDefaultType());
      ((KeyStore)localObject1).load(null, null);
      localObject2 = new MySSLSocketFactory((KeyStore)localObject1);
      localDefaultHttpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", (SocketFactory)localObject2, 443));
    }
    Object localObject1 = null;
    if (paramString3.equals(Method_POST))
    {
      localObject2 = new HttpPost(paramString1);
      ((HttpPost)localObject2).setEntity(new StringEntity(paramString2, "UTF-8"));
      localObject1 = localObject2;
    }
    else if (paramString3.equals(Method_PUT))
    {
      localObject2 = new HttpPut(paramString1);
      ((HttpPut)localObject2).setEntity(new StringEntity(paramString2, "UTF-8"));
      localObject1 = localObject2;
    }
    else if (paramString3.equals(Method_GET))
    {
      localObject1 = new HttpGet(paramString1);
    }
    else if (paramString3.equals(Method_DELETE))
    {
      localObject1 = new HttpDelete(paramString1);
    }
    if (localObject1 != null)
    {
      if (paramMap != null)
      {
        Iterator localIterator = paramMap.entrySet().iterator();
        while (localIterator.hasNext())
        {
          localObject2 = (Map.Entry)localIterator.next();
          ((HttpRequestBase)localObject1).setHeader((String)((Map.Entry)localObject2).getKey(), (String)((Map.Entry)localObject2).getValue());
        }
      }
      localHttpResponse = localDefaultHttpClient.execute((HttpUriRequest)localObject1);
    }
    return localHttpResponse;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.cloud.HttpClientManager
 * JD-Core Version:    0.6.1
 */